﻿using System.Linq;
using Microsoft.TeamFoundation.Build.Client;

namespace BuildExplorer
{
    class CommandLine
    {
        public string CollectionUrl { get; set; }
        public string buildUriOrName { get; set; }
        public bool ShowHelp { get; private set; }

        public CommandLine(string[] args)
        {
            foreach (string arg in args)
            {
                if (arg.StartsWith("/") || arg.StartsWith("-"))
                {
                    int colon = arg.IndexOf(':');
                    if (colon != -1)
                    {
                        CollectionUrl = arg.Substring(colon + 1, arg.Length - colon - 1);
                    }
                    continue;
                }

                buildUriOrName = arg;
            }

            ShowHelp = args.Any() && (string.IsNullOrEmpty(CollectionUrl) || (string.IsNullOrEmpty(buildUriOrName)));
        }

        public static string GetForBuild(IBuildDetail build)
        {
            return string.Format("/collection:{0} {1}",
                build.BuildServer.TeamProjectCollection.Uri,
                build.Uri);
        }

        public override string ToString()
        {
            return "CollectionUrl: " + CollectionUrl + "  buildUriOrName: " + buildUriOrName + "  ShowHelp: " + ShowHelp;
        }
    }
}
